/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: leebean
 * 开发时间: 2019/12/26 17:03
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.config;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.spring.arch.operation.common.enums.Constant;

import feign.RequestInterceptor;
import feign.RequestTemplate;


/**
 * 解决Feign请求Header中HEADER_AUTHORIZATION\HEADER_CLIENT_ID问题
 *
 * @author leebean
 * @version 1.0.0
 * @since 2019/12/26 17:03
 */
@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String headerAuthorization = request.getHeader(Constant.HEADER_AUTHORIZATION);
            if (StringUtils.isNotBlank(headerAuthorization)) {
                headerAuthorization = headerAuthorization.replace(Constant.HEADER_AUTHORIZATION, "").trim();
                requestTemplate.header(Constant.HEADER_AUTHORIZATION, headerAuthorization);
            }

            String headerClientId = request.getHeader(Constant.HEADER_CLIENT_ID);
            if (StringUtils.isNotBlank(headerClientId)) {
                headerClientId = headerClientId.replace(Constant.HEADER_CLIENT_ID, "").trim();
                requestTemplate.header(Constant.HEADER_CLIENT_ID, headerClientId);
            }
        }
    }
}
